<?php

class ArticleModel extends Model{
		
	public function get_article_byid($article_id){
		$sql	= 	"select * from ybr_article where article_id = '{$article_id}' limit 1";
		$datas	=	M()->query($sql);
		empty($datas) ? array() : $datas[0];
	}
	
	public function get_articlelist_bycatid($cat_id){
		$sql	=	"select a1.article_id,a1.cat_id,a1.title from ybr_article a1 left JOIN ybr_article_cat a2 on a2.cat_id = a1.cat_id where a1.cat_id = '{$cat_id}'";
		return M()->query($sql);	
	}
	
	public function get_cat_list($parent_id = 0){
		$sql	=	"select cat_id,cat_name from ybr_article_cat where parent_id = '{$parent_id}' order by sort_order desc";	
		return M()->query($sql);
	}
	
	public function get_list_byname($cat_name){
		if($cat_name == '') return FALSE;
		$sql = "select a1.* from ybr_article a1 left join ybr_article_cat b1 on b1.cat_id = a1.cat_id where b1.cat_name = '{$cat_name}' order by a1.sort_order desc";
		return M()->query($sql);
	}
	
	public function article_info($article_id = null){
		if(empty($article_id)) return array();
		$sql = "select * from ybr_article where article_id = '{$article_id}' limit 1";
		$datas	=	M()->query($sql);
		if(empty($datas)){
			return array();	
		}
						
		$artice_info 	=	$datas[0];
		$file_path		=	"./Public/upload/article/" . $artice_info['src'];
		
		if(file_exists($file_path)){
			$artice_info['content'] = @file_get_contents($file_path);
		}	
		else
			$artice_info['content'] = '<div style="font-size:16px;color:#e0e0e0;text-align:center;">暂无内容！</div>';			
		return $artice_info;
	}
	
	public function related_article($article_id = null){
		if(empty($article_id)) return array();
		
		$CommonModel	=	new CommonModel();
		//上一篇		
		$pre_sql 		= 	"SELECT article_id,title FROM ybr_article  where article_id < $article_id ORDER BY article_id DESC limit 1";
		$pre_article	=	$CommonModel->getRow($pre_sql);
		//下一篇
		$next_sql 		= 	"SELECT article_id,title FROM ybr_article  where article_id > $article_id ORDER BY article_id ASC limit 1";		
		$next_article	=	$CommonModel->getRow($next_sql);
		
		$datas['pre']	=	$pre_article;
		$datas['next']	=	$next_article;
		return $datas;
	}
	
	
	
}























